Programming in Tabled Prolog very DRAFT

نویسنده

  • David S Warren
چکیده

Interpretation Abstract Interpretation examples partial evaluation and assert Show low overhead of tabling in meta interpreter due to how tables are implemented as triesInterpretation examples partial evaluation and assert Show low overhead of tabling in meta interpreter due to how tables are implemented as tries CHAPTER META PROGRAMMING AI of a Simple Nested Procedural Language see warren xsb tests procabsint & In this section we will see how to use XSB to construct a simple abstract interpreter for a pro cedural programming language Such abstract interpreters can be used to do various kinds of data ow analyses The abstract interpretator that we develop here is actually quite sophisticated for example it does interprocedural analysis The interesting part here is how easy and straightforward it is to construct one with XSB and therefore how we can be con dent of its correctness The idea is to rst construct a concrete interpreter for the object language The Prolog pro gramming language makes this particularly easy We can run the concrete interpreter on various object programs and make sure that it is working reasonably well After we have the concrete interpreter we can easily change the operations to be abstract operations that operate over the abstract domain Then we could execute programs over the abstract domain This sounds easy and it is in XSB but a couple of issues arise First when computing over the abstract domain the outcome of conditional tests cannot normally be determined This means that what was a de terministic concrete program becomes a nondeterministic abstract program Since we can t know which branch a specialization of the abstract program might take we have to try them all Now in XSB that is not a problem since XSB supports nondeterminism Second since we can t determine the exact outcome of conditionals we don t know when to exit from loops So a simple execution of most any abstract program would loop in nitely What is really wanted is a least xed point which will give reachable abstract states and tabling in XSB gives exactly that So XSB is ideally suited for this kind of abstract interpretaton problem Consider how this approach works in a speci c case The XSB actually Prolog program shown below is an interpreter for a simple procedural language that supports nested procedures static scoping and call by value parameter passing It is far from trivial and we will discuss how each component works First we discuss how the execution environment is maintained When a procedure is executing it must have access to all variables that are visible to it With each invocation of a procedure there is an activation record AR that stores its local variables This is maintined in our interpreter as a simple list of variable name variable value pairs So when a procedure is executing it will have access to its own AR that stores its local variables But it must also have access to variables global to it i e those in enclosing blocks These are in ARs for the enclosing procedures So the state for a procedure is kept as a list of ARs the rst being the procedure s own AR the second being the AR of the immediately enclosing procedure etc We call such a list of AR s a Stack The following simple predicates get and set variable values in a Stack They take a level number indicating how global the variable is indicates local indicates in the immediately enclosing block etc They also take a Stack and a variable name import append memberchk from basics CHAPTER META PROGRAMMING getVal N Stack Var Val getVal AR Var Val memberchk Var Val AR getVal N AR Stack Var Val N N is N getVal N Stack Var Val setVal N StackIn Var Val StackOut setVal AR StackIn Var Val NAR StackIn repl pair AR Var Val NAR setVal N AR StackIn Var Val AR StackOut N N is N setVal N StackIn Var Val StackOut repl pair Var AR Var Val Var Val AR " repl pair P AR Var Val P NAR repl pair AR Var Val NAR The interpreter takes as input the abstract syntax tree of an object program evaluate a program eval module Name Block evalBlock Block Stack evalBlock block Decls Stmts Pars K Stack Stack remFirst K Stack Stack append Pars Decls Locals evalStmts Stmts Locals Stack Stack addFirst K Stack Stack Stack remFirst L L remFirst N L L N N is N remFirst N L L addFirst Stack Stack addFirst N AR Stack Stack AR Stack N N is N addFirst N Stack Stack Stack evalStmts Stack Stack evalStmts Stmt Stmts Stack Stack evalStmt Stmt Stack Stack evalStmts Stmts Stack Stack evalStmt assign var I Name Exp Stack Stack evalExp Exp Stack Val setVal I Stack Name Val Stack evalStmt while Bool Stmts Stack Stack CHAPTER META PROGRAMMING evalExp Bool Stack BVal BVal Stack Stack evalStmts Stmts Stack Stack evalStmt while Bool Stmts Stack Stack evalStmt if Bool Then Else Stack Stack evalExp Bool Stack BVal BVal evalStmts Then Stack Stack evalStmts Else Stack Stack evalStmt call I Name ActPars Stack Stack getVal I Stack Name proc Forms Body evalPars ActPars Forms Stack ParLocals evalBlock Body ParLocals I Stack Stack evalStmt print Exps Stack Stack eval print exps Exps Stack evalStmt dump Stack Stack writeln Stack nl evalPars Stack evalPars Exp Exps Var Vars Stack Var Val Decls evalExp Exp Stack Val evalPars Exps Vars Stack Decls eval print exps eval print exps Exp Exps Stack evalExp Exp Stack Val writeln Val eval print exps Exps Stack evalExp int V V evalExp var I Name Stack Val getVal I Stack Name Val evalExp op E E Stack V evalExp E Stack V evalExp E Stack V V is V V evalExp op E E Stack V evalExp E Stack V evalExp E Stack V V is V V evalExp op E E Stack V evalExp E Stack V CHAPTER META PROGRAMMING evalExp E Stack V V is V V evalExp op $ E E Stack V evalExp E Stack V evalExp E Stack V V $V V V evalExp op E E Stack V evalExp E Stack V evalExp E Stack V V V V V evalExp op E E Stack V evalExp E Stack V evalExp E Stack V V V V V compose stmt operations evalStmts Stack Stack evalStmts Stmt Stmts Stack Stack evalStmt Stmt Stack Stack evalStmts Stmts Stack Stack extract envs for called block and exec body in that context evalBlock block Decls Stmts Pars Level Stack Stack keepTail Level Stack Stack append Pars Decls Locals evalStmts Stmts Locals Stack Stack replTail Level Stack Stack Stack compute value of an expression in a context evalExp Exp Stack Val evaluate a statement generating new Stack evalStmt assign var I Name Exp Stack Stack evalExp Exp Stack Val setVal I Stack Name Val Stack evalStmt while Bool Stmts Stack Stack evalExp Bool Stack BVal BVal Stack Stack evalStmts Stmts Stack Stack evalStmt while Bool Stmts Stack Stack evalStmt if Bool Then Else Stack Stack evalExp Bool Stack BVal BVal evalStmts Then Stack Stack evalStmts Else Stack Stack evalStmt call I Name ActPars Stack Stack CHAPTER META PROGRAMMING getVal I Stack Name proc Forms Body evalPars ActPars Forms Stack ParLocals evalBlock Body ParLocals I Stack Stack To obtain an abstract interpreter that does uninitialized variable analysis Underline evalExp call in assignment clause to point out that its de nition is changed to im plement abstract operations over the abstract domain of funinitialized hasValueg Constant is mapped to hasValue Binary ops return hasValue if both their operands are hasValue otw unini tialized Add

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Abductive Logic Programming with Tabled Abduction

In abductive logic programming, abductive solutions are typically computed without attending to the abductive context. These abductive solutions can actually be reused in a different abductive context. In this paper, we employ a tabling mechanism and propose a tabled abduction mechanism, that consists of a transformation from abductive normal logic programs into tabled dual programs, by tabling...

متن کامل

Implementing Tabled Abduction in Logic Programs

Abduction has been on the back burner in logic programming, as abduction can be too difficult to implement, and costly to perform, in particular if abductive solutions are not tabled. If they become tabled, then abductive solutions can be reused, even from one abductive context to another. On the other hand, current Prolog systems, with their tabling mechanisms, are mature enough to facilitate ...

متن کامل

Practical Tabled Abduction in Logic Programs

Abduction has by now been employed in various applications using logic programming. But abduction can be costly to perform, if abductive solutions are not tabled, and their reuse allowed even in distinct contexts. Current Prologlike systems, with their tabling mechanisms, are mature enough to facilitate the introduction of tabled abduction into them. This poses a problem of how to actually reus...

متن کامل

A Simple Scheme for Implementing Tabled Logic Programming Systems Based on Dynamic Reordering of Alternatives

Tabled logic programming (LP) systems have been applied to elegantly and quickly solving very complex problems (e.g., model checking). However, techniques currently employed for incorporating tabling in an existing LP system are quite complex and require considerable change to the LP system. We present a simple technique for incorporating tabling in existing LP systems based on dynamically reor...

متن کامل

Approximate Pruning in Tabled Logic Programming

Pruning provides an important tool for control of nondeterminism in Prolog systems. Current Tabled Prolog systems improve Prolog’s evaluation strategy in several ways, but lack satisfactory support for pruning operations. In this paper we present an extension to the evaluation mechanism of Tabled Prolog to support pruning. This extension builds on the concept of demand to select tables to prune...

متن کامل

Achieving Scalability in Parallel Tabled Logic Programs

Tabling or memoing is a technique where one stores intermediate answers to a problem so that they can be reused in further calls. Tabling is of interest to logic programming because it addresses some of most significant weaknesses of Prolog. Namely, it can guarantee termination for programs with the bounded term-size property. Tabled programs exhibit a more complex execution mechanism than trad...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 1999